﻿using System;
using System.Web;
using System.Web.Routing;
using ManagedUIFlow.Infrastructure.Commands;
using ManagedUIFlow.Infrastructure.Services;
using ManagedUIFlow.Model;
using Moq;
using Xunit;

namespace ManagedUIFlow.Specs.Infrastructure.Commands
{
    public class FlowViewSpecs
    {
        [Fact]
        public void RenderViewWithNullFlowThrows()
        {
            Assert.Throws<ArgumentNullException>(
                () =>
                new FlowView(new RequestContext(new Mock<HttpContextBase>().Object, new RouteData())).Execute(null));
        }

        [Fact]
        public void IssueRedirectTicket_AddsExceutionKeyToModelAndExecutesResult()
        {            
            var route = new RouteData();
            route.Values.Add("controller", "search");
            var mockContext = new Mock<HttpContextBase>();
            var mockSession = new Mock<HttpSessionStateBase>();
            mockContext.Setup(m => m.Session).Returns(mockSession.Object);
            var mockResponse = new Mock<HttpResponseBase>();
            mockContext.Setup(c => c.Response).Returns(mockResponse.Object);
            var resquestContext = new RequestContext(mockContext.Object, route);
            var flow = new ManagedFlow(Guid.NewGuid(), "foo", "bar", RedirectBehavior.Manual, "");
            flow.SetNavigation("test");
            var flowViewServiceBuilder = new FlowViewServiceBuilder();
            var flowViewServiceMock = new Mock<IFlowViewService>();
            flowViewServiceBuilder.SetFlowViewService(flowViewServiceMock.Object);
            flowViewServiceMock.Setup(f => f.Serialize(flow, resquestContext)).Returns(flow.Key.ToString);
            var flowView = new FlowView(resquestContext)
                               {
                                   FlowViewServiceBuilder = flowViewServiceBuilder
                               };
            try
            {
                flowView.Execute(flow);
            }
            catch
            {
            }
            Assert.NotNull(flow.ViewData["ExecutionKey"]);
        }
    }
}